local miaoxing = fk.CreateSkill{
  name = "rfenghou_3d9j__miaoxing",
  tags = { Skill.Compulsory }
}

local U = require "packages.utility.utility"

Fk:addQmlMark{
  name = "rfenghou__cardlist",
  how_to_show = function(name, value, player)
    if type(value) ~= "table" then return " " end
    return tostring(#value.list_names)
  end,
  qml_path = "packages/aaa_Romantic/qml/CardListBox",
}

miaoxing:addLoseEffect(function(self, player, is_death)
  if #player:getPile("#rfenghou_3d9j__godzhenji_shuixiang") ~= 0 then
    player:throwAllCards("s", "#rfenghou_3d9j__godzhenji_shuixiang")
  end
  player.room:setPlayerMark(player, "@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang", 0)
end)

miaoxing:addEffect(fk.DrawInitialCards, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(miaoxing.name) and data.num > 0
  end,
  on_use = function (self, event, target, player, data)
    data.num = data.num * 3
  end,
})
miaoxing:addEffect(fk.AfterDrawInitialCards, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(miaoxing.name, true)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = data.num / 3
    local cards = table.simpleClone(player:getCardIds("h"))
    if #cards < 2 * n then return end
    local choices = {}
    local initials = {}
    for i = 0, 2 do
      local markname = Fk:translate("#rfenghou_3d9j__miaoxing_cn_order_" .. i)
      table.insert(choices, markname)
      local card_tabs = {}
      for j = 1, n do
        local cid = cards[i * n + j]
        table.insert(card_tabs, cid)
      end
      table.insert(initials, card_tabs)
    end
    local choice = U.askForChooseCardList(room, player, choices, initials, 2, 2, miaoxing.name, "#rfenghou_3d9j__miaoxing-initial", false, false)
    local mark_table = {
      list_names = {},
      list_cards = {},
      column = 1
    }
    local card_table = {}
    for i, v in ipairs(choice) do
      local ctable = initials[table.indexOf(choices, v)]
      table.insert(mark_table.list_names, "#rfenghou_3d9j__godzhenji_shuixiang_cn_order_" .. i )
      table.insert(mark_table.list_cards, ctable )
      table.insertTable(card_table, ctable)
    end
    player:addToPile("#rfenghou_3d9j__godzhenji_shuixiang", card_table, true, miaoxing.name, player)
    room:setPlayerMark(player, "@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang", mark_table)
  end,
})
miaoxing:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(miaoxing.name) and player.phase == Player.Draw and
      player:getMark("@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang") ~= 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang")
    local names = mark.list_names
    local all_cards = mark.list_cards
    local hands = player:getHandcardNum()
    local choices = {}
    local cards = {}
    for i, cids in ipairs(all_cards) do
      if #cids ~= hands then
        table.insert(choices, names[i])
        table.insert(cards, cids)
      end
    end
    if #cards == 0 then return end
    local choice = U.askForChooseCardList(room, player, choices, cards, 1, #all_cards, miaoxing.name, "#rfenghou_3d9j__miaoxing-draw:::" .. hands, true, false)
    for _, v in ipairs(choice) do
      local ctable = all_cards[table.indexOf(choices, v)]
      local num = hands - #ctable
      if num > 0 then
        --- 神秘摸标记牌女，只能原地造摸牌流程了
        local drawData = DrawData:new{
          who = player,
          num = num,
          skillName = miaoxing.name,
          fromPlace = "top",
        }
        if not room.logic:trigger(fk.BeforeDrawCard, player, drawData) then
          local topCards = room:getNCards(drawData.num, drawData.fromPlace)
          room:moveCards({
            ids = topCards,
            to = drawData.who,
            toArea = Card.PlayerSpecial,
            specialName = "#rfenghou_3d9j__godzhenji_shuixiang",
            moveReason = fk.ReasonDraw,
            proposer = player,
            skillName = drawData.skillName,
          })

          table.insertTable(ctable, topCards)
        end
      elseif num < 0 then
        num = -num
        local discards = room:askToCards(player, {
          min_num = num,
          max_num = num,
          pattern = tostring(Exppattern:new{ id = ctable }),
          expand_pile = ctable,
          skill_name = miaoxing.name,
          prompt = "#AskForDiscard:::" .. num .. ":" .. num,
          cancelable = false,
        })
        room:moveCardTo(discards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, miaoxing.name, nil, nil, player)

        for _, cid in ipairs(discards) do
          table.removeOne(ctable, cid)
        end
      end
    end

    room:setPlayerMark(player, "@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang", mark)
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d9j__miaoxing"] = "淼形",
  [":rfenghou_3d9j__miaoxing"] = "锁定技，分发起始手牌时，共发你三份起始手牌，你将其中两份移出游戏，称为“水相”；摸牌阶段结束时，你须将至少一份“水相”摸至或弃置至手牌数。",

  ["#rfenghou_3d9j__miaoxing-initial"] = "淼形：你须依次将其中两份起始手牌移出游戏，称为“水相”",
  ["#rfenghou_3d9j__miaoxing-draw"] = "淼形：你须将至少一份“水相”摸至或弃置至 %arg 张",

  ["#rfenghou_3d9j__miaoxing"] = "水相",

  ["@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang"] = "水相",
  ["#rfenghou_3d9j__godzhenji_shuixiang_cn_order_1"] = "水相-阳",
  ["#rfenghou_3d9j__godzhenji_shuixiang_cn_order_2"] = "水相-阴",

  ["#rfenghou_3d9j__miaoxing_cn_order_0"] = "淼形-甲",
  ["#rfenghou_3d9j__miaoxing_cn_order_1"] = "淼形-乙",
  ["#rfenghou_3d9j__miaoxing_cn_order_2"] = "淼形-丙",
}

return miaoxing
